package com.sherllinhe.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//import com.sherllinhe.common.context.LoginMemberContext;
import com.sherllinhe.business.domain.*;
import com.sherllinhe.business.enums.SeatColEnum;
import com.sherllinhe.business.req.TrainStationQueryReq;
import com.sherllinhe.business.resp.TrainStationQueryResp;
import com.sherllinhe.common.exception.BusinessException;
import com.sherllinhe.common.exception.BusinessExceptionEnum;
import com.sherllinhe.common.resp.PageResp;
import com.sherllinhe.common.util.SnowUtil;
import com.sherllinhe.business.mapper.TrainCarriageMapper;
import com.sherllinhe.business.req.TrainCarriageQueryReq;
import com.sherllinhe.business.req.TrainCarriageSaveReq;
import com.sherllinhe.business.resp.TrainCarriageQueryResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能：
 * 作者：Sherllin He
 * 日期：2025/5/7 16:48
 */
@Slf4j
@Service
public class TrainCarriageService {
    @Autowired
    private TrainCarriageMapper trainCarriageMapper;
    // 新增和编辑乘客
    public void save(TrainCarriageSaveReq req){
        DateTime now = DateTime.now();

        //自动计算列数和总座位数
        List<SeatColEnum> seatColEnums  = SeatColEnum.getColsByType(req.getSeatType());
        req.setColCount(seatColEnums.size());
        req.setSeatCount(req.getRowCount() * req.getColCount());

        TrainCarriage trainCarriage = BeanUtil.copyProperties(req, TrainCarriage.class);

        //判断是新增还是更新
        //trainCarriage.setMemberId(LoginMemberContext.getId());
        if(ObjUtil.isNull(trainCarriage.getId())) {
            //新增前查询唯一键是否存在
            TrainCarriage trainCarriageDB = selectByUnique(req.getTrainCode(), req.getIndex());
            if(ObjUtil.isNotNull(trainCarriageDB)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CARRIAGE_INDEX_UNIQUE_ERROR);
            }
            //新增
            trainCarriage.setId(SnowUtil.getSnowflakeNextId());
            trainCarriage.setCreateTime(now);
            trainCarriage.setUpdateTime(now);
            trainCarriageMapper.insert(trainCarriage);
        }else{
            //更新
            trainCarriage.setUpdateTime(now);
            trainCarriageMapper.updateByPrimaryKey(trainCarriage);
        }


    }
    // 查询乘客列表
    public PageResp<TrainCarriageQueryResp> queryList(TrainCarriageQueryReq req){
        TrainCarriageExample example = new TrainCarriageExample();
        example.setOrderByClause("train_code asc, `index` asc");
        TrainCarriageExample.Criteria criteria = example.createCriteria();
        //if(ObjUtil.isNotNull(req.getMemberId())){
            //criteria.andMemberIdEqualTo(req.getMemberId());
        //}

        log.info("查询页码:{}",req.getPage());
        log.info("每页条数:{}",req.getSize());
        PageHelper.startPage(req.getPage(),req.getSize());
        List<TrainCarriage> trainCarriageList = trainCarriageMapper.selectByExample(example);

        PageInfo<TrainCarriage> pageInfo = new PageInfo<>(trainCarriageList);
        log.info("总条数:{}", pageInfo.getTotal());
        log.info("总页数:{}", pageInfo.getPages());

        List<TrainCarriageQueryResp> list = BeanUtil.copyToList(trainCarriageList, TrainCarriageQueryResp.class);

        PageResp<TrainCarriageQueryResp> pageResp = new PageResp<>();
        pageResp.setList(list);
        pageResp.setTotal(pageInfo.getTotal());

        return pageResp;
    }
    //删除乘客
    public void delete(Long id){
        trainCarriageMapper.deleteByPrimaryKey(id);
    }

    public List<TrainCarriage> selectByTrainCode(String trainCode){
        TrainCarriageExample example = new TrainCarriageExample();
        example.createCriteria().andTrainCodeEqualTo(trainCode);
        List<TrainCarriage> list = trainCarriageMapper.selectByExample(example);
        return list;
    }

    //查询某车次的车厢信息
    public PageResp<TrainCarriageQueryResp> selectByTrainCode(String trainCode, TrainCarriageQueryReq req){
        TrainCarriageExample example = new TrainCarriageExample();
        TrainCarriageExample.Criteria criteria = example.createCriteria();
        criteria.andTrainCodeEqualTo(trainCode);

        log.info("查询页码:{}",req.getPage());
        log.info("每页条数:{}",req.getSize());
        PageHelper.startPage(req.getPage(),req.getSize());
        List<TrainCarriage> trainCarriageList = trainCarriageMapper.selectByExample(example);

        PageInfo<TrainCarriage> pageInfo = new PageInfo<>(trainCarriageList);
        log.info("总条数:{}", pageInfo.getTotal());
        log.info("总页数:{}", pageInfo.getPages());

        List<TrainCarriageQueryResp> list = BeanUtil.copyToList(trainCarriageList, TrainCarriageQueryResp.class);

        PageResp<TrainCarriageQueryResp> pageResp = new PageResp<>();
        pageResp.setList(list);
        pageResp.setTotal(pageInfo.getTotal());

        return pageResp;
    }
    //  查询唯一键
    private TrainCarriage selectByUnique(String trainCode,Integer index) {
        TrainCarriageExample example = new TrainCarriageExample();
        example.createCriteria().andTrainCodeEqualTo(trainCode).andIndexEqualTo(index);
        List<TrainCarriage> list = trainCarriageMapper.selectByExample(example);
        if(CollUtil.isNotEmpty(list)){
            return list.get(0);
        }else {
            return null;
        }
    }
    // 查询唯一键
    public List<TrainCarriage> selectByUnique(String trainCode) {
        TrainCarriageExample example = new TrainCarriageExample();
        example.createCriteria().andTrainCodeEqualTo(trainCode);
        return trainCarriageMapper.selectByExample(example);

    }
}
